window.onload = initForm;

var province = new Array("请选择","北京市","上海市","天津市","重庆市","安徽省","福建省","甘肅省","广东省","贵州省","海南省","河北省","河南省","黑龙江省","湖北省","湖南省","吉林省","江苏省","江西省","辽宁省","青海省","山东省","山西省","陕西省","四川省","云南省","浙江省","广西壮族自治区","内蒙古自治区","宁夏回族自治区","西藏自治区","新疆维吾尔自治区");
var city = new Array();
var district = new Array();

city["请选择"] = new Array("请选择省");
city["北京市"] = new Array("北京市");
city["上海市"] = new Array("上海市");
city["天津市"] = new Array("天津市");
city["重庆市"] = new Array("重庆市");
city["安徽省"] = new Array("合肥市","淮北市","亳州市","宿州市","蚌埠市","阜阳市","淮南市","滁州市","六安市","马鞍山市","芜湖市","宣城市","铜陵市","池州市","安庆市","黄山市");
city["福建省"] = new Array("福州市","宁德市","南平市","三明市","莆田市","龙岩市","泉州市","漳州市","厦门市");
city["甘肅省"] = new Array("兰州市","嘉峪关市","酒泉市","张掖市","金昌市","武威市","白银市","庆阳市","平凉市","定西市","天水市","陇南市");
city["广东省"] = new Array("广州市","韶关市","梅州市","河源市","清远市","潮州市","揭阳市","汕头市","肇庆市","惠州市","佛山市","东莞市","云浮市","汕尾市","江门市","中山市","深圳市","珠海市","阳江市","茂名市","湛江市");
city["贵州省"] = new Array("贵阳市","遵义市","六盘水市","安顺市","铜仁市","毕节市");
city["海南省"] = new Array("海口市","三亚市","三沙市","儋州市");
city["河北省"] = new Array("石家庄市","保定市","沧州市","承德市","邯郸市","衡水市","廊坊市","秦皇岛市","唐山市","邢台市","张家口市");
city["河南省"] = new Array("郑州市","安阳市","鹤壁市","濮阳市","新乡市","焦作市","三门峡市","开封市","洛阳市","商丘市","许昌市","平顶山市","周口市","漯河市","南阳市","驻马店市","信阳市");
city["黑龙江省"] = new Array("哈尔滨市","黑河市","伊春市","齐齐哈尔市","鹤岗市","佳木斯市","双鸭山市","绥化市","大庆市","七台河市","鸡西市","牡丹江市");
city["湖北省"] = new Array("武汉市","十堰市","襄樊市","随州市","荆门市","孝感市","宜昌市","黄冈市","鄂州市","荆州市","黄石市","咸宁市");
city["湖南省"] = new Array("长沙市","岳阳市","张家界市","常德市","益阳市","湘潭市","株洲市","娄底市","怀化市","邵阳市","衡阳市","永州市","郴州市");
city["吉林省"] = new Array("长春市","白城市","松原市","吉林市","四平市","辽源市","白山市","通化市");
city["江苏省"] = new Array("南京市","连云港市","徐州市","宿迁市","淮安市","盐城市","泰州市","扬州市","镇江市","南通市","常州市","无锡市","苏州市");
city["江西省"] = new Array("南昌市","九江市","景德镇市","上饶市","鹰潭市","抚州市","新余市","宜春市","萍乡市","吉安市","赣州市");
city["辽宁省"] = new Array("沈阳市","铁岭市","阜新市","抚顺市","朝阳市","本溪市","辽阳市","鞍山市","盘锦市","锦州市","葫芦岛市","营口市","丹东市","大连市");
city["青海省"] = new Array("西宁市","海东市");
city["山东省"] = new Array("济南市","德州市","滨州市","东营市","烟台市","威海市","淄博市","潍坊市","聊城市","泰安市","莱芜市","青岛市","日照市","济宁市","菏泽市","临沂市","枣庄市");
city["山西省"] = new Array("太原市","大同市","朔州市","忻州市","阳泉市","晋中市","吕梁市","长治市","临汾市","晋城市","运城市");
city["陕西省"] = new Array("西安市","榆林市","延安市","铜川市","渭南市","宝鸡市","咸阳市","商洛市","汉中市","安康市");
city["四川省"] = new Array("成都市","广元市","巴中市","绵阳市","德阳市","达州市","南充市","遂宁市","广安市","资阳市","眉山市","雅安市","内江市","乐山市","自贡市","泸州市","宜宾市","攀枝花市");
city["云南省"] = new Array("昆明市","昭通市","丽江市","曲靖市","保山市","玉溪市","临沧市","普洱市");
city["浙江省"] = new Array("杭州市","湖州市","嘉兴市","绍兴市","舟山市","宁波市","金华市","衢州市","台州市","丽水市","温州市");
city["广西壮族自治区"] = new Array("南宁市","桂林市","河池市","贺州市","柳州市","百色市","来宾市","梧州市","贵港市","玉林市","崇左市","钦州市","防城港市","北海市");
city["内蒙古自治区"] = new Array("呼和浩特市","呼伦贝尔市","通辽市","赤峰市","巴彦淖尔市","乌兰察布市","包头市","鄂尔多斯市","乌海市");
city["宁夏回族自治区"] = new Array("银川市","石嘴山市","吴忠市","中卫市","固原市");
city["西藏自治区"] = new Array("拉萨市","日喀则市","昌都市","林芝市","山南市","那曲市");
city["新疆维吾尔自治区"] = new Array("乌鲁木齐市","克拉玛依市","吐鲁番市","哈密市");

district["请选择"] = new Array("请选择市");
district["北京市"] = new Array("11区","12区","13区","14区","15区","16区");
district["上海市"] = new Array("21区","22区","23区","24区","25区","26区","27区");
district["天津市"] = new Array("31区","32区","32区","32区","32区","32区");
district["重庆市"] = new Array("41区","42区","42区","42区","42区","42区");
district["合肥市"] = new Array("51区","52区","52区","52区","52区");
district["淮北市"] = new Array("61区","62区","62区","62区","62区","62区");

function initForm() {
	var provinceObj = document.getElementById("province");
	provinceObj.options.length = 0;
	for (var i = 0;i < province.length;i++) {
		provinceObj.options[i] = new Option(province[i]);
		provinceObj.options[i].value = province[i];
	}
	provinceObj.selectedIndex = 0;
	provinceObj.onchange = populateCitys;
}

function populateCitys() {
	var provinceValue = this.value;
	var cityObj = document.getElementById("city");
	cityObj.options.length = 0;
	for (var i in city[provinceValue]) {
		cityObj.options[i] = new Option(city[provinceValue][i]);
		cityObj.options[i].value = city[provinceValue][i];
	}
	cityObj.selectedIndex = 0;
	var cityValue = cityObj.value;
	changeDistricts(cityValue);
	cityObj.onchange = populateDistricts;
}

function populateDistricts() {
	var cityValue = this.value;
	changeDistricts(cityValue);
}

function changeDistricts(cityValue) {
	var districtObj = document.getElementById("district");
	districtObj.options.length = 0;
	for (var i in district[cityValue]) {
		districtObj.options[i] = new Option(district[cityValue][i]);
		districtObj.options[i].value = district[cityValue][i];
	}
}

$(function(){
	var titleName = $(".name");
	var Username = $("#Username");
	var email = $("#email");
	var password = $("#password");
	
	var username = sessionStorage.getItem("loginUser");
	var str = localStorage.getItem(username);
	var user = JSON.parse(str);
	
	titleName.text(user.username);
	Username.val(user.username);
	email.val(user.email);
	password.val(user.password);
});


function validForm() {
	if(checkRegisterName() == false){
		alert("账号请输入6位的英文字母和数字");
		return false;
	} else if(checkEmail() == false){
		alert("请输入正确的邮箱");
		return false;
	} else if(checkPwd() == false){
		alert("密码请输入6位的英文字母和数字");
		return false;
	} else{
		save();
		alert("修改成功！");
		return true;
	}
}

/*验证登录账号*/
function checkRegisterName() {
	var registerName = document.getElementById("Username").value;
	var regRegisterName = /^[a-zA-Z0-9]{6}$/;
	if (regRegisterName.test(registerName) == false) {
		return false;
	}
	return true;
}

// 验证邮箱
function checkEmail(){
	var email = document.getElementById("email").value;
	var regEmail = /^[a-z0-9]\w+@[a-z0-9]{2,3}(\.[a-z]{2,3}){1,2}$/i;
	if(regEmail.test(email) == false) {
		return false;
	}
	return true;
}

/*验证登录密码*/
function checkPwd(){
	var pwd = document.getElementById("password").value;
	var regPwd = /^[a-zA-Z0-9]{6}$/;
	if(regPwd.test(pwd) == false) {
		return false;
	}
	return true;
}

// 检查用户名是否存在
// function checkUsername(username){
// 	for(var i = 0;i < localStorage.length;i++){
// 		// 通过循环拿到每一个key的名字，即username
// 		var keyName = localStorage.key(i);
// 		// 比较localStorage中的用户名和注册时输入的是否一致
// 		if(keyName === username){
// 			alert("用户名已存在");
// 			return false;
// 		}
// 	}
// 	return true;
// }

// 往localStorage中添加用户注册信息
function save(){
	var usernameValue = document.getElementById("Username").value;
	var emailValue = document.getElementById("email").value;
	var passwordValue = document.getElementById("password").value;
	
	var str = localStorage.getItem(usernameValue);
	var user = JSON.parse(str);
	
	user.email = emailValue;
	user.password = passwordValue;
	
	// user对象转换成字符串
	str = JSON.stringify(user);
	localStorage.setItem(user.username,str);
}